﻿using System;
using System.Collections.Generic;
using System.IO;
using static Debugger;

namespace Module.FlowChart.Model
{
    internal sealed class FCLibrary
    {
        public string BasePath { get; private set; }
        public List<FCLibraryItem> Documents;
        //public Action OnReLoad;

        public FCLibrary(string basepath = "FCLibrary\\")
        {
            BasePath = basepath;
            Documents = new List<FCLibraryItem>();
            LoadItemInfos();
        }

        string GetPath(string name)
        {
            return BasePath + name + ".lib";
        }
        void LoadItemInfos()
        {
            //todo 加载库数据
        }

        public string Add(FCLibraryItem item)
        {
            string path = GetPath(item.Name);
            for (int i = 0; i < Documents.Count; i++)
            {
                var doc = Documents[i];
                Assert(doc != item);
                if (doc.Name == item.Name)
                {
                    return "Name Exist";
                }
            }
            if (File.Exists(path))
                return "File Exist";
            Documents.Add(item);
            try
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
                {
                    item.Save(bw);
                    bw.Close();
                }
                return "Success";
            }
            catch (IOException e)
            {
                Documents.Remove(item);//撤销添加
                return "File Create Error";
            }
        }
        public string Delete(FCLibraryItem item)
        {
            for (int i = 0; i < Documents.Count; i++)
            {
                var doc = Documents[i];
                if (doc == item)
                {
                    try
                    {
                        string path = GetPath(doc.Name);
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                    catch (Exception)
                    {
                        return "Error";
                    }
                    Documents.RemoveAt(i);
                    return "Success";
                }
            }
            return "Not Find";
        }
        public string Update(FCLibraryItem item)
        {
            string re = "Success";
            re = Delete(item);
            if (re != "Success")
                return re;
            re = Add(item);
            if (re != "Success")
                return re;
            return re;
        }
        public FCLibraryItem Find(string name)
        {
            for (int i = 0; i < Documents.Count; i++)
            {
                var doc = Documents[i];
                if (doc.Name == name)
                {
                    return doc;
                }
            }
            return null;
        }
        public Document GetDocument(string name)
        {
            BinaryReader br = null;
            var item = Find(name);
            try
            {
                br = new BinaryReader(new FileStream(GetPath(item.Name), FileMode.Open));
                br.BaseStream.Position = item.Offset;
                return Document.LoadFromBytes(br);
            }
            catch (IOException e)
            {
                return null;
            }
        }
        public void ReLoad()
        {
            LoadItemInfos();
            //OnReLoad?.Invoke();
        }


    }

}

